# coding: utf-8
# Author: zhenda

from flask import g, request, current_app
from comment.utils.tokens_pyjwt import verify_tokens


def jwt_request_authorization():
    """
    定义请求钩子， 请求进来，先验证token
    :return: 验证成功后，把用户id放入全局变量g中
    """
    g.user_id = None
    try:
        token = request.headers.get('token')
    except Exception as e:
        current_app.logger.info('请求头中没有token', e)
        return
    rst = verify_tokens(token)
    if 'id' in rst:
        g.user_id = rst['id']
